﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.DataVisualization.Charting;

namespace WebReport.Reports
{
    public partial class DocPerformance : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Pager.PageChange += new PagerControl.PageRefresh(Pager_PageChange);
            InitChart();
            BindChart();
            if (!Page.IsPostBack)
            {
                BindGrid(0);
            }
        }

        void Pager_PageChange(int pageIndex)
        {
            BindGrid(pageIndex);
        }
        protected void InitChart()
        {
            SplineChart.Titles.Add("Doc Performance Chart");
            //SplineChart
        }
        protected void BindChart()
        {
            using (DefaultDBEntity db = new DefaultDBEntity())
            {
                var query = from p in db.WordMonitor
                            group p by p.StartTimeFormat into g
                            select new
                            {
                                StartTimeFormat = g.Key,
                                LoadAppTime = g.Average(t => t.LoadAppTime),
                                LoadTemplateTime = g.Average(t => t.LoadTemplateTime),
                                WriteTime = g.Average(t => t.WriteTime),
                                SaveTime = g.Average(t => t.SaveTime),
                                CloseTime = g.Average(t => t.CloseTime)
                            };
                var list = query.ToList();

                Series serLoadDocTime = CreateSeries("LoadDocTime");
                Series serLoadTemplateTime = CreateSeries("LoadTemplateTime");
                //Series serWriteTime = CreateSeries("WriteTime");
                Series serSaveTime = CreateSeries("SaveTime");
                Series serCloseTime = CreateSeries("CloseTime");

                foreach (var item in list)
                {
                    string startTime = item.StartTimeFormat.Value.ToString("yyyy/MM/dd HH:mm");
                    AppendDatePoint(serLoadDocTime
                        , startTime
                        , item.LoadAppTime.Value);

                    AppendDatePoint(serLoadTemplateTime
                        , startTime
                        , item.LoadTemplateTime.Value);

                    //AppendDatePoint(serWriteTime
                    //    , startTime
                    //    , item.WriteTime.Value);

                    AppendDatePoint(serSaveTime
                        , startTime
                        , item.SaveTime.Value);

                    AppendDatePoint(serCloseTime
                        , startTime
                        , item.CloseTime.Value);

                }

                SplineChart.Series.Add(serLoadDocTime);
                SplineChart.Series.Add(serLoadTemplateTime);
                //SplineChart.Series.Add(serWriteTime);
                SplineChart.Series.Add(serSaveTime);
                SplineChart.Series.Add(serCloseTime);
            }

        }

        protected Series CreateSeries(string name)
        {
            Series s = new Series(name);
            s.ChartType = SeriesChartType.StackedColumn;
            s.BorderWidth = 2;
            s.IsVisibleInLegend = true;

            return s;
        }

        protected void AppendDatePoint(Series s, string axisLabe, params object[] yValue)
        {
            DataPoint dp = new DataPoint();
            dp.AxisLabel = axisLabe;
            dp.SetValueY(yValue);
            //serLoadDoc.Points.Add(dpLoadDoc);
            //return dp;
            s.Points.Add(dp);
        }
        protected void BindGrid(int pageIndex)
        {
            using (DefaultDBEntity db = new DefaultDBEntity())
            {

                var query = from p in db.WordMonitor
                            orderby p.WordMonitorID
                            select p;
                var list = query.Skip(pageIndex * PageSize).Take(PageSize).ToList();
                gvAction.DataSource = list;
                gvAction.DataBind();

                Pager.IntialProperties(query.Count());
                Pager.CurrentPageIndex = pageIndex;
            }
        }

        private const int PageSize = 10;

    }
}